package com.cn.hotel.servlet.back;

import com.cn.hotel.model.UserModel;
import com.cn.hotel.service.IUserService;
import com.cn.hotel.service.impl.UserServiceImpl;
import redis.clients.jedis.Jedis;
//import com.sun.xml.ws.developer.servlet.HttpSessionScope;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /**
         * 处理乱码（因为用户输入全是英文或者数字  所以不用处理乱码问题）
         * 得到页面的值
         * 调用服务逻辑层
         * 得到数据
         * 返回结果
         * */
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String code = req.getParameter("code");
        UserModel userModel = new UserModel();
        userModel.setUsername(username);
        userModel.setPassWord(password);
        userModel.setCode(code);
        String sessionCode = (String) req.getSession().getAttribute("code");//得到code的值
        userModel.setSessionCode(sessionCode);
        IUserService iUserService = new UserServiceImpl();
        int result = iUserService.login(userModel);
        if (result == 1){
//            req.getSession().setAttribute("username",username);  //把username存在session
//            把username存到radius
            Jedis jedis = new Jedis();
            jedis.set(username,username);
        }
        resp.getWriter().println(result);
    }
}
